﻿using NDF.Utilities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NDF.Data.EntityFramework
{
    public static class DbSetExtensions
    {
        public static void AddOrUpdate<TEntity>(this IDbSet<TEntity> _this, object entity) where TEntity : class
        {
            AddOrUpdate<TEntity>(_this, entity.CastTo<TEntity>());
        }

        public static void AddOrUpdate<TEntity>(this IDbSet<TEntity> _this, TEntity entity) where TEntity : class
        {
            DbSetMigrationsExtensions.AddOrUpdate<TEntity>(_this, entity);
        }


        public static void AddOrUpdateRange<TEntity>(this IDbSet<TEntity> _this, IEnumerable<object> entities) where TEntity : class
        {
            AddOrUpdateRange<TEntity>(_this, entities.Select(entity => entity.CastTo<TEntity>()));
        }

        public static void AddOrUpdateRange<TEntity>(this IDbSet<TEntity> _this, params object[] entities) where TEntity : class
        {
            AddOrUpdateRange<TEntity>(_this, entities.AsEnumerable());
        }


        public static void AddOrUpdateRange<TEntity>(this IDbSet<TEntity> _this, IEnumerable<TEntity> entities) where TEntity : class
        {
            DbSetMigrationsExtensions.AddOrUpdate<TEntity>(_this, entities.ToArray());
        }

        public static void AddOrUpdateRange<TEntity>(this IDbSet<TEntity> _this, params TEntity[] entities) where TEntity : class
        {
            AddOrUpdateRange<TEntity>(_this, entities.AsEnumerable());
        }
    }
}
